package q002;

import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Queue<Integer> s1 = new LinkedBlockingDeque<>();
        Queue<Integer> s2 = new LinkedBlockingDeque<>();
        Queue<Integer> s3 = new LinkedBlockingDeque<>();

        //遍历l1
        while (l1 != null) {
            s1.add(l1.val);
            l1 = l1.next;
        }
        //遍历l2
        while (l2 != null) {
            s2.add(l2.val);
            l2 = l2.next;
        }
        ListNode dummyHead = new ListNode(0);
        ListNode head = dummyHead;

        int size = s1.size() > s2.size() ? s1.size() : s2.size();
        int jw = 0;
        for (int i = 0 ; i < size ; i++) {
            int num1=0,num2=0;
            if (!s1.isEmpty()) {
                num1 = s1.remove();
            }
            if (!s2.isEmpty()) {
                num2 = s2.remove();
            }
            int r = (num1 + num2 + jw) % 10;
            s3.add(r);
            jw = (num1 + num2 + jw) / 10;
        }
        if (jw != 0)
            s3.add(jw);



        size = s3.size();
        for ( int i = 0 ; i < size ; i++) {
            head.next = new ListNode(s3.remove());
            head = head.next;
        }

        return dummyHead.next;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] arr1 = {1,8};
        int[] arr2 = {0};
        ListNode listNode = solution.addTwoNumbers(new ListNode(arr1), new ListNode(arr2));
        System.out.println();

    }
}


